package com.jfinal.weixin.controller;

import com.jfinal.core.Controller;
import com.jfinal.log.Log4jLog;
import com.jfinal.weixin.model.User;
import com.jfinal.weixin.model.WeixinInfo;
import com.jfinal.weixin.sdk.api.*;

import java.util.Map;

/**
 * Created by 446705260@qq.com on 16/5/7.
 * chengzhichao
 * 装逼朋友圈模块
 */
public class ZbController extends Controller {
    private static WeixinInfo weixinInfo = WeixinInfo.dao.getWeixinInfoById(1);

    public ApiConfig getApiConfig() {
        ApiConfig ac = new ApiConfig(weixinInfo);
        return ac;
    }


    public void index() {
        //获取URL后方openid参数,确定被访者信息.
        String openid = getPara("openid");
        String sence = getPara("sence");
        String sex = getPara("sex");
        if (sence == null || sence.equals("null")) {
            sence = "a";
        }
        if (sex == null || sex.equals("null")) {
            sex = "man";
        }

        //如果未携带该参数
        User user = User.dao.getUserByOpenid(openid);
        if (user != null) {
            Map paraMap = JsTicketApi.makeSignature(getRequest(), this.getApiConfig());
            setAttr("map", paraMap);
            setAttr("zbuser", user);
            render(sex + sence + ".html");
        } else {
            renderText("请在微信公众号'红安人'上操作");
        }


    }

    /**
     * 用户切换查看不同朋友圈场景的时候触发此方法
     * 如果在数据库中存在该用户的用户信息,则不需要授权
     * 如果没有该用户信息,则引导授权
     */
    public void duang() {
        String openid = getPara("openid");
        String sence = getPara("sence");
        String sex = getPara("sex");
        if (sence == null || sence.equals("null")) {
            sence = "a";
        }
        if (sex == null || sex.equals("null")) {
            sex = "man";
        }
        if (openid == null || openid.equals("null")) {
            openid = "erro";
        }
        /**
         * 检查用户微信是否存在cookieopenid
         * 如果有,则数据库中肯定可以拿到该信息
         */
        String cookieopenid = getCookie("cookieopenid", null);
        if (User.dao.getUserByOpenid(cookieopenid) != null) {
            String url =  weixinInfo.get("host")+"/zb?openid=" + cookieopenid + "&sence=" + sence + "&sex=" + sex;
            redirect(url);
        } else {
            String url = SnsAccessTokenApi.getAuthorizeURL(getApiConfig().getAppId(),
                    weixinInfo.get("host")+ "/zb/doAuthforuser?sence=" + sence + "&sex=" + sex, false);
            redirect(url);
        }

    }

    public void jiehun() {
        String openid = getPara("openid", "");
        Map paraMap = JsTicketApi.makeSignature(getRequest(), this.getApiConfig());
        paraMap.put("sharetitle","对不起,现在才通知大家,5月20号,我结婚了");
        paraMap.put("shareimg",weixinInfo.get("host")+"/jiehun/xi.jpg");
        paraMap.put("sharedesc","来喝喜酒吧,就在二小城东母婴生活馆对面那里,二楼、11点不见不散哈");
        paraMap.put("sharesuccess","红安人正在紧急开发更多有趣好玩的新功能,请持续关注,还有红包放送");
        paraMap.put("sharefail","哈是红安人,捧个场撒!");
        setAttr("map", paraMap);

        setAttr("picsrc", weixinInfo.get("host")+"/jiehun/" + openid + ".jpg");
        render("/jiehun/index.html");
    }

    public void doup(){
        Map paraMap = JsTicketApi.makeSignature(getRequest(), this.getApiConfig());
        setAttr("map", paraMap);
        render("/doup/index.html");
    }

    public void doAuthforuser() {
        String code = getPara("code");
        String sence = getPara("sence");
        String sex = getPara("sex");
        if (sence == null || sence.equals("null")) {
            sence = "a" ;
        }
        if (sex == null || sex.equals("null")) {
            sex = "man";
        }
        if (code == null) {
            renderText("CODE参数错误" + "--------446705260@QQ.COM");
        } else {
            SnsAccessToken st = SnsAccessTokenApi.getSnsAccessToken(this
                    .getApiConfig().getAppId(), this.getApiConfig()
                    .getAppSecret(), code);
            String accessToken = st.getAccessToken();
            String myopenid = st.getOpenid();
            User user = new User();
            if (user.isJoined(myopenid) > 0) {
                user = user.getUserByOpenid(myopenid);
            } else {
                ApiResult ar = SnsApi.getUserInfo(accessToken, myopenid);
                user.set("openid", ar.get("openid").toString());
                user.set("headimgurl", ar.get("headimgurl").toString());
                user.set("nickname", ar.get("nickname").toString());
                user.set("city", ar.get("city").toString());
                user.set("subscribe", "3");
                user.save();
            }
            setCookie("cookieopenid", user.getStr("openid"), 36000);

            redirect(weixinInfo.get("host")+"/zb?openid="
                    + user.getStr("openid") + "&sence=" + sence + "&sex=" + sex);

        }

    }
}
